<template>
	<xn-form-container
		:title="formData.id ? '编辑训练数据表' : '增加训练数据表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="问题描述：" name="question">
				<a-textarea v-model:value="formData.question" placeholder="请输入问题描述" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
			<a-form-item label="关联的SQL语句：" name="sqlText">
				<a-textarea v-model:value="formData.sqlText" placeholder="请输入关联的SQL语句" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
			<a-form-item label="关联的DDL语句：" name="ddlText">
				<a-textarea v-model:value="formData.ddlText" placeholder="请输入关联的DDL语句" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
			<a-form-item label="类型：" name="dataType">
				<a-select v-model:value="formData.dataType" placeholder="请选择类型" :options="dataTypeOptions" />
			</a-form-item>
			<a-form-item label="相关文档说明：" name="document">
				<a-textarea v-model:value="formData.document" placeholder="请输入相关文档说明" :auto-size="{ minRows: 3, maxRows: 5 }" />
			</a-form-item>
			<a-form-item label="是否积极示例：" name="isPositiveExample">
				<a-radio-group v-model:value="formData.isPositiveExample" placeholder="请选择是否积极示例" :options="isPositiveExampleOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="trainDataForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import trainDataApi from '@/api/ai/trainDataApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const dataTypeOptions = ref([])
	const isPositiveExampleOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		dataTypeOptions.value = tool.dictList('traindatatype')
		isPositiveExampleOptions.value = tool.dictList('status')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		dataType: [required('请输入类型')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				trainDataApi
					.trainDataSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
